package com.tgy.数组和集合转换;

import java.util.*;
import java.util.stream.Collectors;

/***
 * @ClassName: trans
 * @Description: 集合和数组相互转换
 * @Auther: 送一一块二向箔
 * @Date: 17:38 2021/11/16
 * @version : V1.0
 */
//集合转为数组
//Collection.toArray(T[] a)
//数组转为集合
//Arrays.toList(T…a)
public class trans {
    public static void main(String[] args) {
        Integer[] arrsIntreger = {1, 2};
        /*
         * @Param [args]
         * @Description: 数组转集合
         * Arrays.toList(T…a)
         * @auther: TangGuoyi
         * @date: 17:41 2021/11/16
         * @return: void
         */
        //1.遍历
        List<Integer> list1 = new ArrayList<>();
        for (int ele : arrsIntreger) {
            list1.add(ele);
        }
        System.out.println(list1);


        //无法对基本类型数组进行转换
        List<Integer> list2 = Arrays.asList(arrsIntreger);
        System.out.println(list2);

        int[] arrsInt = new int[2];
        arrsInt[0] = 3;
        arrsInt[1] = 4;
        List<Integer> list3 = Arrays.stream(arrsInt).boxed().collect(Collectors.toList());
        System.out.println(list3);


        /*
         * @Param [args]
         * @Description: 集合转数组
         * Collection.toArray(T[] a)
         * @auther: TangGuoyi
         * @date: 17:57 2021/11/16
         * @return: void
         */

        //1、遍历
        List<Integer> list4 = new ArrayList<Integer>();
        list4.add(5);
        list4.add(6);
        int[] arrsInt1 = new int[list4.size()];
        for (int i = 0; i < list4.size(); i++) {
            arrsInt1[i] = list4.get(i);
        }
        //遍历输出数组
        for (Integer i : arrsInt1) {
            System.out.println(i);

            //2、使用 Collection.toArray(T…a)
            Integer[] arrsIntreger1 = new Integer[list4.size()];
            list4.toArray(arrsIntreger1);
            for (Integer j : arrsIntreger1) {
                System.out.println(j);
            }

            Iterator it = list4.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }
//        System.out.println(it.next());
            System.out.println(it.hasNext());
        }
    }
}
